package com.service.Impl;

import com.constant.Constant;
import com.entity.Course;
import com.entity.CourseType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.CourseMapper;
import com.mapper.CourseTypeMapper;
import com.service.CourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
public class CourseTypeServiceImpl implements CourseTypeService {

    @Autowired
    CourseTypeMapper courseTypeMapper;

    @Autowired
    CourseMapper courseMapper;

    @Override
    public List<CourseType> findCtAll() {
        List<CourseType> courseTypes = courseTypeMapper.selectctAll();
        System.out.println(courseTypes+"courseTypes");
        return courseTypes;
    }

    @Override
    @Transactional
    public CourseType findCourseByParentId(Integer parent_id) {
        CourseType courseType = courseTypeMapper.selectCourseByParentId(parent_id);
        Set<CourseType> childSet = courseTypeMapper.selectChildSet(parent_id);
        courseType.setChildrens(childSet);
        return courseType;
    }

    @Override
    public List<CourseType> showCosTypeName() {
        List<CourseType> courseTypes = courseTypeMapper.selectCourses();
        return courseTypes;
    }

    @Override
    public PageInfo<CourseType> showCosTypeByPage(Integer pageNo, Integer parent_id) {

        if(pageNo==null){
            PageHelper.startPage(1, Constant.PAGE_SIZE_COURSE);
        }else {
            PageHelper.startPage(pageNo, Constant.PAGE_SIZE_COURSE);
        }
        List<CourseType> courseTypes = courseTypeMapper.seleDefCourseTypes(parent_id);
        PageInfo<CourseType> pageInfo = new PageInfo<>(courseTypes);
        return pageInfo;
    }

    @Override
    public Integer addCourseType(String type_name, Integer parent_id, Integer status) {

        List<CourseType> courseTypes = courseTypeMapper.selectAllTypeName();
        for(CourseType item: courseTypes){
           if(type_name.equals(item.getType_name())) {
               return -1;
            }
        }
        Integer integer = courseTypeMapper.addNewCourseType(type_name, parent_id, status);
        return integer;
    }

    @Override
    public Integer changeCoutype(String type_name, Integer parent_id) {
//        System.out.println("type_name---------------------------------"+type_name+"parent_id------"+parent_id);
        Integer integer = courseTypeMapper.updateParentIdByTypename(type_name, parent_id);
//        System.out.println("changeCoutype---------------------------------"+integer);
        return integer;
    }

    @Override
    public Integer chageCourseTypeStatus(Integer id, Integer status) {
        Integer integer = courseTypeMapper.updateCourseTypeStatus(id, status);
        return integer;
    }
}
